﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace ACLCTaguigEnrollmentSystem.UserControls
{
    public partial class uc_SettingSubjects : UserControl
    {
        public uc_SettingSubjects()
        {
            InitializeComponent();
        }

        SqlConnection conn = new SqlConnection(Classes.ConnectionString.connString);

        private void uc_SettingSubjects_Load(object sender, EventArgs e)
        {

        }


        private void tsb_LaboratoryTypeSave_Click(object sender, EventArgs e)
        {

        }

        private void dgv_LaboratoryType_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            DataGridView dgv = sender as DataGridView;
            if (dgv == null)
                return;
            if (dgv.CurrentRow.Selected)
            {
                if (e.RowIndex >= 0)
                {
                    DataGridViewRow row = this.dgv_LaboratoryType.Rows[e.RowIndex];
                    Classes.Variable.LaboratoryTypeID = row.Cells["LaboratoryTypeID"].Value.ToString();
                    SqlCommand cmd = new SqlCommand("SELECT LaboratoryTypeID, LaboratoryTypeCode, LaboratoryTypeName FROM LaboratoryType WHERE LaboratoryTypeID = @LaboratoryTypeID", conn);
                    cmd.Parameters.AddWithValue("@LaboratoryTypeID", Classes.Variable.LaboratoryTypeID);

                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    DataTable dt = new DataTable();
                    da.Fill(dt);
                    conn.Open();
                    txt_LabaratoryTypeCode.Text = dt.Rows[0].ItemArray[1].ToString();
                    txt_LabaratoryTypeName.Text = dt.Rows[0].ItemArray[2].ToString();
                    txt_LabDescription.Text = dt.Rows[0].ItemArray[3].ToString();

                    conn.Close();
                }
            }
        }

#region Methods

        #region Data Population
        private void LaboratoryTypeDataPopulation()
        {
            SqlCommand cmd = new SqlCommand("SELECT LaboratoryTypeID, LaboratoryTypeName FROM LaboratoryType", conn);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);

            conn.Open();
            cb_LabType.DataSource = ds.Tables[0];
            cb_LabType.SelectedValue = "LaboratoryTypeID";
            cb_LabType.SelectedText = "LaboratoryTypeName";

            conn.Close();

        }

        private void dgvLaboratory()
        {
            SqlCommand cmd = new SqlCommand("SELECT LaboratoryTypeID, LaboratoryTypeCode, LaboratoryTypeName FROM LaboratoryType", conn);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);

            dgv_LaboratoryType.DataSource = dt;
        }
        #endregion

        #region Data
        #region  Laboratory Type Data
        private void AddLaboratoryType()
        {
            SqlCommand cmd = new SqlCommand("dbo.spLaboratoryType_Insert", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlDataAdapter da = new SqlDataAdapter(cmd);

            conn.Open();

            cmd.Parameters.AddWithValue("@LaboratoryTypeCode", txt_LabaratoryTypeCode.Text);
            cmd.Parameters.AddWithValue("@LaboratoryTypeName", txt_LabaratoryTypeName.Text);
            cmd.Parameters.AddWithValue("@LaboratoryTypeDesc", txt_LabDescription.Text);
            
            cmd.ExecuteNonQuery();

            conn.Close();
        }

        private void UpdateLaboratoryType()
        {
            SqlCommand cmd = new SqlCommand("dbo.spLaboratoryType_Update", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlDataAdapter da = new SqlDataAdapter(cmd);

            conn.Open();
            cmd.Parameters.AddWithValue("@LaboratoryTypeID", Classes.Variable.LaboratoryTypeID);
            cmd.Parameters.AddWithValue("@LaboratoryTypeCode", txt_LabaratoryTypeCode.Text);
            cmd.Parameters.AddWithValue("@LaboratoryTypeName", txt_LabaratoryTypeName.Text);
            cmd.Parameters.AddWithValue("@LaboratoryTypeDesc", txt_LabDescription.Text);
            cmd.ExecuteNonQuery();
            conn.Close();
        }
        #endregion

        #region Subject Data
        private void InsertSubject()
        {
            if (chkb_Lecture.Checked)
            {
                SqlCommand cmd = new SqlCommand("dbo.spSubjectsLec_Insert", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                SqlDataAdapter da = new SqlDataAdapter(cmd);

                conn.Open();

                cmd.Parameters.AddWithValue("@SubjectCode", txt_SubjectCode.Text);
                cmd.Parameters.AddWithValue("@SubjectName", txt_SubjectName.Text+" Lec");
                cmd.Parameters.AddWithValue("@SubjectDesc", txt_Description.Text);
                cmd.Parameters.AddWithValue("@Units", txt_LectureUnits.Text);
                cmd.Parameters.AddWithValue("@SubjectTypeID", "Lecture");

                conn.Close();

            }

            if (chkb_Laboratory.Checked)
            {
                SqlCommand cmd = new SqlCommand("dbo.spSubjects_Insert", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                SqlDataAdapter da = new SqlDataAdapter(cmd);

                conn.Open();

                cmd.Parameters.AddWithValue("@SubjectCode", txt_SubjectCode.Text+"L");
                cmd.Parameters.AddWithValue("@SubjectName", txt_SubjectName.Text+" Lab");
                cmd.Parameters.AddWithValue("@SubjectDesc", txt_Description.Text);
                cmd.Parameters.AddWithValue("@Units", txt_LaboratoryUnits.Text);
                cmd.Parameters.AddWithValue("@SubjectTypeID", "Laboratory");
                cmd.Parameters.AddWithValue("@LaboratoryTypeID", cb_LabType.SelectedValue);

                conn.Close();
            }

        }

        #endregion

        #endregion


#endregion

    }
}
